<?php

namespace App\HttpController\Admin;

use App\Model\AdminsModel;
use App\Utility\Pool\MysqlObject;
use App\Utility\Pool\MysqlPool;
use EasySwoole\Utility\Hash;
use EasySwoole\Validate\Validate;

class OperateController extends AuthController {

    public function index(){
        $request= $this->request();
        $page   = $request->getRequestParam('page') ?: 1;
        $kwd    = $request->getRequestParam('kwd') ?: '';
        $list = MysqlPool::invoke(function (MysqlObject $db) use($kwd,$page){
            $model  = new AdminsModel($db);
            $list   = $model->where(function ($query) use($kwd){
                    if($kwd){
                        $query->where('username','%'.$kwd.'%','like')
                            ->whereOr('nickname','%'.$kwd.'%','like');
                    }
                })
                ->orderBy('created_at','ASC')
                ->paginate($page);

            $this->stateToText($list['data'],[
                'status' => $model::STATUS_TEXT,
            ]);
            return $list;
        });
        return $this->writeJson(0,$list);
    }

    public function update(){
        $request = $this->request();
        $data = $request->getRequestParam();
        MysqlPool::invoke(function (MysqlObject $db) use($data){
            $model = new AdminsModel($db);
            $validate = new Validate();
            $validate->addColumn('username')->required('请填写管理员账号')->func(function () use($data,$model){
                return empty($model->where('id',$data['id'],'<>')->where('username',$data['username'])->first());
            },'管理员账号已存在');
            $validate->addColumn('password')->func(function () use($data){
                if($data['password'] && (strlen($data['password'])<6)){
                    return false;
                }
                return true;
            },'密码长度不能小于6位数');
            $validate->addColumn('nickname')->required('请填写管理员昵称');
            if($this->validate($validate)){
                if($data['id']){
                    if($data['password']){
                        $data['password'] =  Hash::makePasswordHash($data['password']);
                    }else{
                        unset($data['password']);
                    }
                    $model->update($data);
                    return $this->writeJson(0,null,'编辑管理员信息成功');
                }else{
                    //新增
                    $data['password'] = $data['password'] ? Hash::makePasswordHash($data['password']) : Hash::makePasswordHash('123456');
                    $data['created_at'] = date('Y-m-d H:i:s',time());
                    $data['login_at'] = date('Y-m-d H:i:s',time());
                    $model->create($data);
                    return $this->writeJson(0,null,'新增管理员信息成功');
                }
            }else{
                return $this->writeJson(1, null, $validate->getError()->__toString());
            }
        });
    }

    public function destroy(){
        $request = $this->request();
        $id = (int)$request->getRequestParam('id');
        if(!$id){
            return $this->writeJson(1,null,'参数错误');
        }
        MysqlPool::invoke(function (MysqlObject $db) use($id){
            $model = new AdminsModel($db);
            $model->destroy($id);
            return $this->writeJson(0);
        });
    }

}
